﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Model;
using AiXiu.Common;
using AiXiu.IBLL;
using AiXiu.DAL;
using AiXiu.IDAL;

namespace AiXiu.BLL
{
    /// <summary>
    /// 登录注册业务逻辑
    /// </summary>
    public class LoginAndRegisterBLL : ILoginAndRegisterBLL
    {
        /// <summary>
        /// 实例化登录数据访问
        /// </summary>
        private ILoginAndRegisterDAL loginDAL = new LoginAndRegisterDAL();

        public OperResult<TBUsers> loginByUserName(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                return OperResult<TBUsers>.Failed("用户名或密码不能为空");
            }
            
            var login = loginDAL.getLoginByUserName(userName);
            if (login == null)
            {
                return OperResult<TBUsers>.Failed("用户名不存在");
            }
            else if (!login.Password.Equals(new SHAEncryption().SHA1Encrypt(password)))
            {
                return OperResult<TBUsers>.Failed("密码错误");
            }
            else 
            {
                IUserService userInfo = new UserService();
                return OperResult<TBUsers>.Succeed(userInfo.getUserById(login.Id));
            }
        }

        public OperResult<TBUsers> loginByMobileNumber(string mobileNumber, string password)
        {
            if (string.IsNullOrWhiteSpace(mobileNumber) || string.IsNullOrWhiteSpace(password))
            {
                return OperResult<TBUsers>.Failed("手机号或密码不能为空");
            }

            var login = loginDAL.getLoginByMobileNumber(mobileNumber);
            if (login == null)
            {
                return OperResult<TBUsers>.Failed("手机号未注册");
            }
            else if (!login.Password.Equals(new SHAEncryption().SHA1Encrypt(password)))
            {
                return OperResult<TBUsers>.Failed("密码错误");
            }
            else
            {
                IUserService userInfo = new UserService();
                return OperResult<TBUsers>.Succeed(userInfo.getUserById(login.Id));
            }
        }
        public OperResult<TBUsers> register(TBLogins reg)
        {
            if (string.IsNullOrWhiteSpace(reg.UserName)) 
                return OperResult<TBUsers>.Failed("用户名不能为空");
            else if (string.IsNullOrWhiteSpace(reg.Password))
                return OperResult<TBUsers>.Failed("密码不能为空");
            else if (string.IsNullOrWhiteSpace(reg.MobileNumber))
                return OperResult<TBUsers>.Failed("手机号码不能为空");
            
            var loginInfo = loginDAL.getLoginByUserName(reg.UserName);
            if (loginInfo != null)
                return OperResult<TBUsers>.Failed("用户名已存在");
            if (loginDAL.getLoginByMobileNumber(reg.MobileNumber) != null)
                return OperResult<TBUsers>.Failed("手机号已被注册");

            reg.Password = new SHAEncryption().SHA1Encrypt(reg.Password);
            try
            {
                int? id = loginDAL.register(reg);
                if (id != null)
                {
                    IUserService userInfo = new UserService();
                    return OperResult<TBUsers>.Succeed(userInfo.getUserById(Convert.ToInt32(id)));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex + "  --注册用户");
                return OperResult<TBUsers>.Wrong(ex, "注册失败：" + ex.Message);
            }
            return OperResult<TBUsers>.Failed("注册失败");
        }

        public OperResult<TBLogins> getLoginById(int id)
        {
            return OperResult<TBLogins>.Succeed(loginDAL.getLoginById(id));
        }
    }
}
